//
//  TIMGroupAdministratorVC.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/7/9.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMGroupAdministratorVC.h"
#import "TIMAddGroupTabCell.h"
#import "TIMDemoMacro.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"

@interface TIMGroupAdministratorVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *friendData;
@property (nonatomic, strong) NSMutableArray *selectArray;

@property (nonatomic, strong) TIMGroupInfo *groupInfo;

@property (nonatomic, assign) BOOL isSelected;

@end

@implementation TIMGroupAdministratorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectArray = [NSMutableArray array];
    self.isSelected = YES;
    [self setUpNav];
    [self.view addSubview:self.tableView];
}

- (void)setUpNav
{
    self.navigationItem.title = @"群管理";
    self.view.backgroundColor = UIColor.whiteColor;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 65, 25);
    btn.titleLabel.font = TPSFontSize(15);
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(finishClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)finishClicked {
    if (self.selectArray.count == 0) {
        return;
    }
    
    if (self.managerGroup == 1) {
        [TIMApi operateGroupWithGroupId:self.groupId operateUserId:nil MsgOperation:TIMOperateGroupTypeAddAdminGroup groupMembers:self.selectArray GroupName:[self.groupInfo.groupName dataUsingEncoding:NSUTF8StringEncoding] completion:^(NSError *error) {
            if (!error) {
                NSLog(@"------xiadeli-------TIMGroupAdministratorVC-------------添加管理员成功ok");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        }];
    } else if (self.managerGroup == 0) {
        [TIMApi operateGroupWithGroupId:self.groupId operateUserId:nil MsgOperation:TIMOperateGroupTypeTransferOwner groupMembers:self.selectArray GroupName:[self.groupInfo.groupName dataUsingEncoding:NSUTF8StringEncoding] completion:^(NSError *error) {
            if (!error) {
                NSLog(@"------xiadeli-------TIMGroupAdministratorVC-------------转让群主成功ok");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        }];
    }
}

#pragma mark - Delegate

-  (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.friendData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIde=@"cellIde";
    TIMAddGroupTabCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIde];
    if (cell == nil) {
        cell= [[TIMAddGroupTabCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIde];
    }
    
    TIMUserInfo *user = self.friendData[indexPath.row];
    cell.nameLabel.text = user.showName;
    [cell.headImageView setImage:[UIImage imageNamed:@"tab_mine_unselect"]];
    cell.selectBtn.selected = user.isSelect;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TIMUserInfo *user = self.friendData[indexPath.row];
    [user setValue:@(!user.isSelect) forKey:@"isSelect"];
    if (user.isSelect) {
        [self.selectArray addObject:user];
    } else {
        [self.selectArray removeObject:user];
    }
    
    [self.tableView reloadData];
}

#pragma mark - Get

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0.0, 0, Screen_Width, Screen_Height) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSectionIndexBackgroundColor:[UIColor clearColor]];
        [_tableView setSectionIndexColor:[UIColor darkGrayColor]];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 62, 0, 0)];
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView setTableFooterView:v];
    }
    return _tableView;
}

- (NSArray *)friendData {
    if (!_friendData) {
        // 获取群成员
        _friendData = [self.groupInfo.users copy];
    }
    return _friendData;
}

- (TIMGroupInfo *)groupInfo {
    if (!_groupInfo) {
        _groupInfo = [[TIMStorageManager sharedStorageManager] getGroupInfoByGroupID:self.groupId];
    }
    return _groupInfo;
}

@end
